﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Win32;
using System.Diagnostics;

namespace AddingEntriesToContextMenu.Forms
{
    public partial class Main : Form
    {
        #region Properties
        private IEContextMenu IeContextMenu { get; set; }
        #endregion

        #region Ctor
        public Main()
        {
            InitializeComponent();
            IEContextMenu.MenuKeysRefrsh += IEContextMenu_MenuKeysRefrsh;
            
            // Call to the method becose the event can not resie in static constractor
            IEContextMenu_MenuKeysRefrsh(null, EventArgs.Empty);
            cmbExists.Select();
        }

        #endregion

        #region Events
        private void btnSave_Click(object sender, EventArgs e)
        {
            if (CheckValidators())
            {
                IeContextMenu = new IEContextMenu(cmbExists.Text);
                IeContextMenu.Title = txtMenuText.Text;
                IeContextMenu.Url = txtFileName.Text;
                IeContextMenu.Flag = cbFlags.Checked;
                IeContextMenu.Context = GetContextsFromUser();

                string title = IeContextMenu.Title;
                IeContextMenu.Save();

                MessageBox.Show(string.Format("{0} was save", title));
            }
        }

        private void cmbExists_SelectedIndexChanged(object sender, EventArgs e)
        {
            ClearFields();
            
            if (cmbExists.SelectedIndex > 0)
            {
                SetFields(cmbExists.Text);
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (IeContextMenu == null)
            {
                MessageBox.Show("Please choose a Contex Menu to delete", "IEContexMenu", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            if (MessageBox.Show("Are you sure you want to delete", 
                "IEContexMenu", 
                MessageBoxButtons.YesNo, 
                MessageBoxIcon.Warning, 
                MessageBoxDefaultButton.Button2) == DialogResult.Yes)
            {
                IeContextMenu.Delete();
            }

        }

        private void IEContextMenu_MenuKeysRefrsh(object sender, EventArgs e)
        {
            cmbExists.Items.Clear();
            cmbExists.Items.Add("Add new menu");
            cmbExists.Items.AddRange(IEContextMenu.MenuKeys);
            cmbExists.SelectedIndex = 0;
        }

        #region Help Menu
        private void llOpenFile_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                txtFileName.Text = string.Format(@"file://{0}", openFileDialog1.FileName);
            }
        }

        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            new AboutBox().ShowDialog();
        }

        private void mSDNToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenLink("http://msdn.microsoft.com/en-us/library/aa753589(VS.85).aspx");
        }

        private void codeProjectCreatingAndInstallingInternetExplorerContextMenuUsingCToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenLink("http://www.codeproject.com/KB/menus/IE_Context_Menu_Installer.aspx");
        }

        private void myBlogHebrewDeleteToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenLink("http://blogs.microsoft.co.il/blogs/shlomo/archive/2009/03/04/cancel-the-captcha.aspx");
        }

        private void mSDNBlogExtendingTheInternetExplorerContextMenuToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenLink("http://blogs.msdn.com/oldnewthing/archive/2004/05/24/140283.aspx");
        }

        private void myToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenLink("http://blogs.microsoft.co.il/blogs/shlomo/archive/2008/12/28/internet-explorer-addin.aspx");
        }

        private void descriptionOnTheFieldsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            new Description().ShowDialog();
        }
        #endregion

        #endregion

        #region Method

        private IEContextMenu.Contexts GetContextsFromUser()
        {
            List<IEContextMenu.Contexts> context = new List<IEContextMenu.Contexts>();

            CheckedListBox.CheckedItemCollection collection = clbContexts.CheckedItems;

            for (int i = 0; i < collection.Count; i++)
            {
                context.Add((IEContextMenu.Contexts)
                            Enum.Parse(typeof(IEContextMenu.Contexts),
                            collection[i].ToString()));
            }

            for (int i = 1 ; i < context.Count; i++)
            {
                context[0] |= context[i];
            }


            return context[0];
        }

        private bool CheckValidators()
        {
            bool result = true;

            #region Menu Text Required
            if (txtMenuText.Text.Trim() == string.Empty)
            {
                ep1.SetError(txtMenuText, "Menu text is required");
                result = false;
            }
            else
            {
                #region Menu Text Exist
                if (txtMenuText.Text != cmbExists.Text)
                {
                    if (IEContextMenu.MenuKeys.Contains(txtMenuText.Text))
                    {
                        ep1.SetError(txtMenuText, "The menu text exist");
                        result = false;
                    }
                    else
                    {
                        ep1.SetError(txtMenuText, string.Empty);
                    }
                }
            }
            #endregion

            
            #endregion

            #region File Name Required
            if (txtFileName.Text.Trim() == string.Empty)
            {
                ep1.SetError(txtFileName, "File name is required");
                result = false;
            }
            else
            {
                ep1.SetError(txtFileName, string.Empty);
            }
            #endregion

            #region Contex Required
            if (clbContexts.CheckedItems.Count == 0)
            {
                ep1.SetError(clbContexts, "Context is required");
                result = false;
            }
            else
            {
                ep1.SetError(clbContexts, string.Empty);
            }
            #endregion

            return result;
        }

        private void ClearFields()
        {
            IeContextMenu = null;

            txtMenuText.Text = string.Empty;
            txtFileName.Text = string.Empty;
            for (int i = 0; i < clbContexts.Items.Count; i++)
            {
                clbContexts.SetItemChecked(i, false);
            }
            cbFlags.Checked = false;

            ep1.Clear();
        }

        private void SetFields(string menuText)
        {
            IeContextMenu = IEContextMenu.FromRegistry(menuText);
            txtMenuText.Text = IeContextMenu.Title;
            txtFileName.Text = IeContextMenu.Url;
            cbFlags.Checked = IeContextMenu.Flag;

            for (int i = 0; i < clbContexts.Items.Count; i++)
            {
                IEContextMenu.Contexts context = 
                    (IEContextMenu.Contexts)Enum.Parse(typeof(IEContextMenu.Contexts), 
                    clbContexts.Items[i].ToString());

                if ((IeContextMenu.Context & context) > 0)
                {
                    clbContexts.SetItemChecked(i, true);
                }
            }
        }

        private void OpenLink(string link)
        {
            Process.Start(link);
        }
        #endregion 
       
    }
}
